Skip to content

fix: surface provider content filter and preserve context tokens - #963

Merged
7Sageer merged 2 commits into
MoonshotAI:mainfrom
7Sageer:fix/provider-filtered-silent-ignore
Jun 22, 2026
Merged

fix: surface provider content filter and preserve context tokens#963
7Sageer merged 2 commits into
MoonshotAI:mainfrom
7Sageer:fix/provider-filtered-silent-ignore

Conversation

@7Sageer

@7Sageer 7Sageer commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator

Related Issue

No related issue — this PR fixes a bug observed in a user session where provider safety policy triggers were silently swallowed.

Problem

When the provider returns content_filter / finishReason: 'filtered', the agent loop currently detects it but the turn.ended event is emitted with reason: 'completed'. The TUI/CLI gives no indication that the response was blocked, so users see the agent simply stop responding and may repeatedly prompt "continue".

Additionally, the context token accounting in AgentContext overwrites _tokenCount with the current step's usage total. For a filtered step the usage is all zeros, so the displayed context token count drops to 0 after the filter.

What changed

  • Added 'filtered' to TurnEndReason in the protocol schema and wired it through agent/turn so turn.ended accurately reports the reason.
  • Updated the TUI and CLI prompt mode to surface provider safety-policy blocks with a clear notice/error instead of treating the turn as successful.
  • Updated the web UI event projector and the ACP adapter to treat 'filtered' as an abnormal turn end (error/aborted).
  • Fixed prompt/session lifecycle services so 'filtered' is treated like other non-completed turns.
  • Fixed AgentContext so a step reporting zero usage (e.g. content filter) no longer zeros the accumulated context token count; it falls back to estimating from the covered messages.
  • Added unit tests for the new filtered-turn mapping and the zero-usage context-token behavior, and generated a changeset for the CLI.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Jun 22, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 1580bb9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Jun 22, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@1580bb9
npx https://pkg.pr.new/@moonshot-ai/kimi-code@1580bb9

commit: 1580bb9

@itxaiohanglover itxaiohanglover left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well-structured fix — surfacing the 'filtered' reason instead of silently treating it as completed is the right behavior. The TUI notice with the raw finish reason is helpful for debugging. Changeset included.

7Sageer added 2 commits June 22, 2026 17:23
- context: accumulate token estimate for zero-usage steps to preserve
  the tokenCount / tokenCountCoveredMessageCount invariant
- turn/goal: pause the goal when a turn is blocked by safety policy
- subagent: surface a filtered child turn as a distinct error
- acp: map filtered to the native ACP refusal stop reason
- tui: show a filtered-specific message in the btw panel
- cli: drop the redundant content_filter suffix from the error message
- tests: cover filtered across cli, web, acp, and goal flows
@7Sageer
7Sageer force-pushed the fix/provider-filtered-silent-ignore branch from bc49d28 to 1580bb9 Compare June 22, 2026 09:26
@7Sageer
7Sageer merged commit 4292ae9 into MoonshotAI:main Jun 22, 2026
8 checks passed
@github-actions github-actions Bot mentioned this pull request Jun 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants